// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageFrameConcatAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Vision.Runtime
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.Runtime;
    using Microsoft.Robotics.Vision.Cameras;
    using Microsoft.Robotics.Vision.Cameras.Webcam;
    using Microsoft.Robotics.Vision.Runtime.Cameras;

    /// <summary>
    /// Agent concatenates RGB camera messages into a single image. The cameras are assumed
    /// to form a rectangular matrix in the X Y plane. Images in a column must have identical widths. Likewise images in
    /// a row have the same height. When setting up the agent just the number of columns and rows is specified.
    /// The agent must be then configured with input camera agents that lie in a grid pattern
    /// The positions of the input cameras is provided by the poses on the messages the cameras send. The incoming agent
    /// message should also have non null camera ID. The agent does not care about the absolute camera positions
    /// only the relative positions in the X Y plane. At run time the relative locations in the grid are inferred from the poses.
    /// The agent should be configured to cache this mapping when relative camera locations will not change.
    /// The position and offsets for each camera is contained in the "interest patch" for each camera message.
    /// </summary>
    [DataContract]
    public class RGBImageFrameConcatAgent : ProducerAgent<RGBCameraAgentMessage>, ISubscriptionReceiver<RGBCameraAgentMessage>
    {
        /// <summary>
        /// Fractional threshold used for checking incoming camera configurations correspond to a grid
        /// </summary>
        private readonly double gridThresholdFraction = 0.01;

        /// <summary>
        /// Names of the agents producing the input messages
        /// </summary>
        [DataMember(Name = "Producers")]
        private string[] producers;

        /// <summary>
        /// Count of producers in horizontal direction
        /// </summary>
        [DataMember(Name = "CountX")]
        private int countX;

        /// <summary>
        /// Count of producers in vertical direction
        /// </summary>
        [DataMember(Name = "CountY")]
        private int countY;

        /// <summary>
        /// When true allows the agent to cache the mapping between relative locations of source cameras and the grid
        /// Enable this when then relative positions of source cameras will not change
        /// </summary>
        [DataMember(Name = "EnableCameraMapCache")]
        private bool enableCameraMapCache;

        /// <summary>
        /// Patches of rectangles
        /// </summary>
        private Rectangle[,] patchRectangles;

        /// <summary>
        /// Images to patch together
        /// </summary>
        private ImageFrameYuy2[,] images;

        /// <summary>
        /// Count of unique images received
        /// </summary>
        private int uniqueCount;

        /// <summary>
        /// Used to temporarily cache received message till all expected messages have arrived
        /// </summary>
        private Dictionary<string, RGBCameraAgentMessage> receivedMessageCache;

        /// <summary>
        /// maps camera ID to (row) ordered relative locations of camera on the grid
        /// </summary>
        private List<string> cameraMapping;

        /// <summary>
        /// Initializes a new instance of the <see cref="RGBImageFrameConcatAgent"/> class. 
        /// </summary>
        /// <param name="name">Agent name</param>
        /// <param name="producers">Names of input producers</param>
        /// <param name="countX">Count of horizontal images</param>
        /// <param name="countY">Count of vertical images</param>
        /// <param name="enableCameraMapCache">
        /// When true allows agent to cache the relative locations of source cameras
        /// Enable this when then relative positions of source cameras will not change 
        /// </param>
        public RGBImageFrameConcatAgent(string name, string[] producers, int countX, int countY, bool enableCameraMapCache)
            : base(name)
        {
            this.producers = producers;
            this.countX = countX;
            this.countY = countY;
            this.enableCameraMapCache = enableCameraMapCache;
            if (countX * countY != this.producers.Length)
            {
                throw new ArgumentException(string.Format("Expected {0} * {1} = {2} producers, found {3}", countX, countY, countX * countY, producers.Length));
            }
        }

        /// <summary>
        /// Called when the agent is about to start running
        /// </summary>
        public override void OnActivated()
        {
            base.OnActivated();
            this.patchRectangles = new Rectangle[this.countX, this.countY];
            this.images = new ImageFrameYuy2[this.countX, this.countY];
            this.receivedMessageCache = new Dictionary<string, RGBCameraAgentMessage>();
        }

        /// <summary>
        /// Finds and subscribes to all publishing agent.
        /// </summary>
        /// <param name="locator">A locator to use.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            foreach (string producer in this.producers)
            {
                IAgentHost agentHost = locator.GetAgent(producer);
                agentHost.Subscribe<RGBCameraAgentMessage>(this);
            }
        }

        /// <summary>
        /// Handles reception of the RGBCameraAgentMessage message 
        /// Received messages are cached for each cell in the rectangular matrix
        /// When messages have been received for all cells the messages
        /// are concatenated into a single image and posted to the output. The cache is cleared and accumulation
        /// begins again.
        /// </summary>
        /// <param name="message">Received camera message</param>
        public void Receive(RGBCameraAgentMessage message)
        {
            // messages are required to have cameraId's. Allow Null exception if the ID is missing
            if (false == this.receivedMessageCache.ContainsKey(message.CameraId))
            {
                this.receivedMessageCache.Add(message.CameraId, message);
                ++this.uniqueCount;
            }
            else
            {
                this.receivedMessageCache[message.CameraId] = message;
            }

            if (this.uniqueCount == this.producers.Length)
            {
                // All cells in the matrix have been received, go ahead 
                // to build the concatenated image and post result
                this.Post();
            }
        }

        /// <summary>
        /// Concatenates received images and posts the result
        /// </summary>
        private void Post()
        {
            if (null == this.cameraMapping)
            {
                this.cameraMapping = this.BuildMapping();
            }

            int idx = 0;
            for (int y = 0; y < this.countY; ++y)
            {
                for (int x = 0; x < this.countX; ++x)
                {
                    RGBCameraAgentMessage msg = this.receivedMessageCache[this.cameraMapping[idx]];
                    ++idx;
                    this.images[x, y] = msg.RGBImageFrame;
                    this.patchRectangles[x, y] = msg.InterestPatch;
                }
            }

            ImageFrame<Yuy2Pix> image = ImageOperations2D.ConcatenateImages<Yuy2Pix>(this.patchRectangles, this.images);
            RGBCameraAgentMessage postMsg = new RGBCameraAgentMessage(AgentMessage.GetCurrentTime(), image);
            this.Publisher.Post(postMsg);
            this.uniqueCount = 0;
            this.receivedMessageCache.Clear();

            if (false == this.enableCameraMapCache)
            {
                this.cameraMapping.Clear();
            }
        }

        /// <summary>
        /// rebuild the mapping from received message ID to ordered locations in the grid
        /// <remarks>This is relatively expensive operation so results will usually be cached</remarks>
        /// </summary>
        /// <returns>Row major ordered list of message ID</returns>
        private List<string> BuildMapping()
        {
            Dictionary<string, Pose> poseDict = new Dictionary<string, Pose>();
            foreach (KeyValuePair<string, RGBCameraAgentMessage> kvp in this.receivedMessageCache)
            {
                poseDict[kvp.Key] = kvp.Value.CameraPose;
            }

            List<string> map = PoseOperations2D.SortPoseDictByRowOrder(poseDict);
            if (false == PoseOperations2D.IsPoseListGrid(poseDict, map, this.countX, this.countY, this.gridThresholdFraction))
            {
                throw new ArgumentException(
                    string.Format(
                    "RGBImageFrameConcatAgent: Pose of incoming web cam messages failed the grid check for countx {0} countY {1}",
                    this.countX,
                    this.countY));
            }

            return map;
        }
    }
}
